#ifndef _CONTAINER_RUN_HPP_
#define _CONTAINER_RUN_HPP_

#ifdef	__cplusplus

#include <dlcom/unknown.h>
#include <utilex/calldll.hpp>
#include <dlcom/loadcom.hpp>

class CContainer
{
public:
	CContainer() {
	
	}
	virtual ~CContainer() {

		m_pIObjectRun.dispose();
		m_loader.UnloadCom();
	}

	HRESULT CreateContainer(const basic_tchar* path,UINT len) {
		
		HRESULT hr = S_OK;

		m_pIObjectRun.dispose();
		m_loader.UnloadCom();

		basic_tchar szContainerName[DYNAMIC_NAME_LEN + 1] = { 0x00 };
		GetDynamicName(_T("container"), szContainerName, DYNAMIC_NAME_LEN);

		m_strContainer.clear();
		m_strContainer.append(path);
		m_strContainer.append(path_slash);
		m_strContainer.append(szContainerName);

		hr = m_loader.LoadCom(m_strContainer.c_str());
		rc_assert(hr == S_OK, E_FAIL)

		hr = m_loader.CreateInstance(CLSID_CObjectRun, INULL, INULL, IID_IObjectRun, (void**)&m_pIObjectRun);
		rc_assert(((hr == S_OK) && (m_pIObjectRun != INULL)), E_FAIL)

		hr = m_pIObjectRun->Clear();
		rc_assert(hr == S_OK, E_FAIL)

		hr = m_pIObjectRun->SetPath(path, len);
		rc_assert(hr == S_OK, E_FAIL)

		return S_OK;
	}
	HRESULT RegisterContainer(LPCSTR buf, ULONG len) {

		rc_assert(m_pIObjectRun != NULL, E_FAIL)
		return m_pIObjectRun->RegisterCode(buf, len);
	}

	HRESULT ExitContainer(_pinstance instance, UINT exit) {

		rc_assert(m_pIObjectRun != NULL, E_FAIL)
		return m_pIObjectRun->NotifyExit(instance, exit);
	}
	
	HRESULT InitContainer(_pinstance instance, basic_tchar* argv[], int argc) {

		rc_assert(m_pIObjectRun != NULL, E_FAIL)
		return m_pIObjectRun->Init(instance, argv, argc);
	}

	HRESULT StartContainer(_pinstance instance, UINT type) {

		rc_assert(m_pIObjectRun != NULL, E_FAIL)
		return m_pIObjectRun->Start(instance, type);
	}

	HRESULT StopContainer(_pinstance instance, UINT type, UINT exit) {

		rc_assert(m_pIObjectRun != NULL, E_FAIL)
		return m_pIObjectRun->Stop(instance, type, exit);
	}

	HRESULT RegisterComponent(REFCLSID clsid, LPCSTR progId, IBase* pBase, ULONG Id, LPCSTR Component, UINT type) {
		
		rc_assert(m_pIObjectRun != NULL, E_FAIL)
		rc_assert(progId != NULL, E_FAIL)
		rc_assert(pBase != NULL, E_FAIL)

		return m_pIObjectRun->AddObject(clsid, progId, pBase, Id, Component, type);
	}
	
	HRESULT RunContainer(_pinstance instance, basic_tchar* argv[], int argc, UINT type) {

		rc_assert(m_pIObjectRun != NULL, OBJECT_RUN_RET_ERROR)
		UINT code = OBJECT_RUN_RET_SUCCESS;

		HRESULT hr = S_OK;

		logi("m_pIObjectRun->Start==>%u",type);
		hr = m_pIObjectRun->Start(instance, type);
		rc_assert(hr == S_OK, E_FAIL)

		code = m_pIObjectRun->GetExitCode();

		logi("m_pIObjectRun->Stop==>%u", code);
		return m_pIObjectRun->Stop(instance, type, code);
	}

	HRESULT UnInitContainer(_pinstance instance) {

		rc_assert(m_pIObjectRun != NULL, S_ERROR)
		return m_pIObjectRun->Uninit(instance);
	}

	UINT GetContainerExitCode() {

		rc_assert(m_pIObjectRun != NULL, OBJECT_RUN_RET_ERROR)
		return m_pIObjectRun->GetExitCode();
	}

	ULONG GetStartIndex(ULONG index) {

		rc_assert(m_pIObjectRun != NULL, OBJECT_RUN_RET_ERROR)
		return m_pIObjectRun->GetRunIndex(index);
	}

	ULONG GetCompentCount() {

		rc_assert(m_pIObjectRun != NULL, OBJECT_RUN_RET_ERROR)
		return m_pIObjectRun->GetRunPluginsCount();
	}


private:
	CComLoader				m_loader;
	basic_tstring			m_strContainer;
	_lComPtr<IObjectRun>	m_pIObjectRun;
};

#endif

#endif 
